package team.kas.anime.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.kas.anime.pojo.Result;
import team.kas.anime.pojo.User;
import team.kas.anime.service.UserService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/verifyUsername")
    @ResponseBody
    public Result verifyUsername(String username){
        return userService.verifyUsername(username);
    }

    @RequestMapping("/codeVarify")
    @ResponseBody
    public Result codeVarify(String mobileCode, HttpServletRequest request){
        Result result = new Result();
        Integer code1 = (Integer) request.getSession().getAttribute("mobileCode");
        if(code1 != null){
            String code = code1.toString();
            if(mobileCode.equals(code)){
                return result;
            }else {
                result.setCode(500);
                result.setMsg("验证码过期或有误！");
                return result;
            }
        }else {
            result.setCode(500);
            result.setMsg("清先获取验证码！");
            return result;
        }
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(User user, String mobileCode){

            return userService.register(user);
    }
    @ResponseBody
    @RequestMapping("/list")
    public Result list(Integer page,Integer limit){

        return userService.userlist(page,limit);
    }
    @RequestMapping("/user")
    public String user(){

        return "user/user-list";
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(String id){
        return userService.deleteBykey(id);
    }

    @ResponseBody
    @RequestMapping("/bulkdelete")
    public Result bulkdelete(String[] ids){

        return userService.bulkdelete(ids);
    }

    @ResponseBody
    @RequestMapping("/detail")
    public User detail(HttpServletRequest request){
        String id= (String) request.getSession().getAttribute("userid");
        return userService.selectById(id);
    }
    @RequestMapping("/search")
    @ResponseBody
    public Result selectByLike(String value,Integer page,Integer limit){
        Result result = userService.selectByLike(value,page,limit);
        return result;
    }
    @RequestMapping("/detaillist")
    public String detaillist(String id,HttpServletRequest request){
        request.getSession().setAttribute("userid",id);
        return "user/user-detail";
    }
    @PostMapping("/update")
    @ResponseBody
    public Result update(String id,String  value,String field){
        Result result=new Result();
        result=userService.update(id,value,field);
        return result;
    }
    @GetMapping("/add")
    public String add(){
        return "user/user-detail";
    }

    @ResponseBody
    @PostMapping("/adduser")
    public Result adduser(User user){
        return userService.adduser(user);
    }

    @ResponseBody
    @RequestMapping("/imgupload")
    public Result imgupload(MultipartFile file, HttpServletRequest request){
        Result result=new Result();
        result=userService.imgupload(file,request);
        return result;
    }

}
